const getNumberGroupMap = require("../../awakened-potential/modules/cjs/getNumberGroupMap.js");

const checkAccuracy = require("./checkAccuracy.js");

const { allNumbers } = require("./constant.js");

const getKillPreviousGroupAccuracy = (allValues, groupsObject) => {
  // 📦 引入生成号码与子分组映射的工具
  const numberGroupMap = getNumberGroupMap(allNumbers, groupsObject);

  const groupAccuracyMap = {};

  const groupKeys = Object.keys(groupsObject);

  // 遍历每一期和下一期，组成 pair 判断杀号准确性
  for (let i = 1; i < allValues.length; i++) {
    const prevItem = allValues[i - 1];
    const currentItem = allValues[i];
    //
    const prevNumber = prevItem.number;

    const currentNumber = currentItem.number;

    groupKeys.forEach((groupName) => {
      if (!groupAccuracyMap[groupName]) {
        groupAccuracyMap[groupName] = [];
      }

      // 获取「前一期号码」所在的子分组索引
      const prevSubGroupIndex = numberGroupMap[prevNumber]?.[groupName];

      // 获取「前一期号码」所在的子分组数据
      const prevSubGroup = groupsObject[groupName][prevSubGroupIndex];

      // 判断「当前号码」是否被「前一期号码」所在的子分组「杀中」
      const isAccurate = checkAccuracy(prevSubGroup, currentNumber);

      // 记录准确性（1 表示杀对，0 表示杀错）
      groupAccuracyMap[groupName].push(isAccurate);
    });
  }
  return groupAccuracyMap;
};

module.exports = getKillPreviousGroupAccuracy;
